package com.wjm.blog.core.http;

import com.alibaba.fastjson.JSONObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;

@Configuration
public class RestTemplateConfig {

    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setConnectTimeout(30000);
        factory.setReadTimeout(30000);
        return factory;

    }

    /**
     * 解决RestTemplate 请求接收不了自定义错误
     *
     * @return RestTemplate
     */
//  使用restTemplate 时，设置restTemplate bean 名为注入时起的名字
//  @Resource(name = "facePlusRestTemplate")  private RestTemplate restTemplate;
    @Bean
    public RestTemplate facePlusRestTemplate() {
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setConnectTimeout(300000);
        requestFactory.setReadTimeout(300000);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        restTemplate.getMessageConverters().add(new FormHttpMessageConverter());
        restTemplate.setErrorHandler(new FacePlusThrowErrorHandler());
        return restTemplate;
    }

    public static void main(String[] args) {
        String url = "http://10.194.4.144/U9C/CustRestServices/YY.U9.Cust.CommonSV.ICommonOperateSV.svc/Do";
        RestTemplate restTemplate = new RestTemplate();
        String paeam = "{\"actionType\":\"Add\",\"businessType\":\"SO\",\"context\":{\"CultureName\":\"zh-CN\",\"OrgCode\":\"20\",\"UserCode\":\"admin\",\"EntCode\":\"01\"},\"requestData\":\"<Body><Item><Head><segment9>CNY</segment9><segment11>9264</segment11><segment8>SZSHS3HHHT20221228-0002QT</segment8><segment10></segment10><segment7>XMHHHTXQFQT20221214-01</segment7><segment13>HHHT</segment13><segment6>19</segment6><segment12>0</segment12><segment5></segment5><segment15>CST-221205-0002</segment15><segment4>CST-221205-0002</segment4><segment14>EU-NONE</segment14><segment3>2023-01-04 14:58:12</segment3><segment2></segment2><segment1>SO01</segment1></Head><Details><Detail><segment17></segment17><segment16>无</segment16><segment19>BSR-230104-0005</segment19><segment18></segment18><segment9>XMHHHTXQFQT20221214-01</segment9><segment11>326074</segment11><segment8>28000.00</segment8><segment10>SZSHS3HHHT20221228-0002QT</segment10><segment7>24778.76</segment7><segment13>2023-01-04 14:58:12</segment13><segment6>XMHHHTXQFQT20221214-01</segment6><segment12>0.00000</segment12><segment5>13%</segment5><segment15>2023-01-04 14:58:12</segment15><segment4>2800.000000</segment4><segment14>2023-01-04 14:58:12</segment14><segment3>2800.000000</segment3><segment2>10.00000</segment2><segment1>07.01.06.01.140068</segment1></Detail><Detail><segment17></segment17><segment16>无</segment16><segment19>BSR-230104-0005</segment19><segment18></segment18><segment9>XMHHHTXQFQT20221214-01</segment9><segment11>326077</segment11><segment8>210000.00</segment8><segment10>SZSHS3HHHT20221228-0002QT</segment10><segment7>185840.71</segment7><segment13>2023-01-04 14:58:12</segment13><segment6>XMHHHTXQFQT20221214-01</segment6><segment12>0.00000</segment12><segment5>13%</segment5><segment15>2023-01-04 14:58:12</segment15><segment4>35000.000000</segment4><segment14>2023-01-04 14:58:12</segment14><segment3>35000.000000</segment3><segment2>6.00000</segment2><segment1>07.01.06.01.140071</segment1></Detail><Detail><segment17></segment17><segment16>无</segment16><segment19>BSR-230104-0005</segment19><segment18></segment18><segment9>XMHHHTXQFQT20221214-01</segment9><segment11>326075</segment11><segment8>18800.00</segment8><segment10>SZSHS3HHHT20221228-0002QT</segment10><segment7>16637.17</segment7><segment13>2023-01-04 14:58:12</segment13><segment6>XMHHHTXQFQT20221214-01</segment6><segment12>0.00000</segment12><segment5>13%</segment5><segment15>2023-01-04 14:58:12</segment15><segment4>1880.000000</segment4><segment14>2023-01-04 14:58:12</segment14><segment3>1880.000000</segment3><segment2>10.00000</segment2><segment1>07.01.06.01.140069</segment1></Detail><Detail><segment17></segment17><segment16>无</segment16><segment19>BSR-230104-0005</segment19><segment18></segment18><segment9>XMHHHTXQFQT20221214-01</segment9><segment11>326076</segment11><segment8>15200.00</segment8><segment10>SZSHS3HHHT20221228-0002QT</segment10><segment7>13451.33</segment7><segment13>2023-01-04 14:58:12</segment13><segment6>XMHHHTXQFQT20221214-01</segment6><segment12>0.00000</segment12><segment5>13%</segment5><segment15>2023-01-04 14:58:12</segment15><segment4>1900.000000</segment4><segment14>2023-01-04 14:58:12</segment14><segment3>1900.000000</segment3><segment2>8.00000</segment2><segment1>07.01.06.01.140070</segment1></Detail><Detail><segment17></segment17><segment16>无</segment16><segment19>BSR-230104-0005</segment19><segment18></segment18><segment9>XMHHHTXQFQT20221214-01</segment9><segment11>326078</segment11><segment8>12920.00</segment8><segment10>SZSHS3HHHT20221228-0002QT</segment10><segment7>11433.63</segment7><segment13>2023-01-04 14:58:12</segment13><segment6>XMHHHTXQFQT20221214-01</segment6><segment12>0.00000</segment12><segment5>13%</segment5><segment15>2023-01-04 14:58:12</segment15><segment4>170.000000</segment4><segment14>2023-01-04 14:58:12</segment14><segment3>170.000000</segment3><segment2>76.00000</segment2><segment1>07.99.00.00.002589</segment1></Detail></Details></Item></Body>\"}";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
        HttpEntity<String> request = new HttpEntity<>(paeam, headers);
        ResponseEntity<String> postForEntity = restTemplate.postForEntity(url, request, String.class);
        System.out.println("发送ERP数据结果返回:{}" + JSONObject.toJSONString(postForEntity.getBody()));
    }
}
